​Bitwise XOR Operator (^)

The ^ operator (also known as the XOR operator) stands for Exclusive Or. Here, if bits in the compared position do not match their resulting bit is 1. i.e, The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite, otherwise 0.

Example: 

Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y

Bitwise OR of (7 ^ 4)

Explanation: On the basis of truth table of bitwise XOR operator we can conclude that the result of 

1 ^ 1  = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

We used the similar concept of bitwise operator that are show in the image.

Implementation of XOR operator:

C++




#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 12, b = 25;
    cout << (a ^ b);
    return 0;
}


Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 12, b = 25;
        int result = a ^ b;
        System.out.println(result);
    }
}
 
// This code is contributed by garg28harsh.


Python3




a = 12
b = 25
result = a ^ b
print(result)
 
# This code is contributed by garg28harsh.


C#




// C# Code
 
using System;
 
public class GFG {
 
    static public void Main()
    {
 
        // Code
        int a = 12, b = 25;
        int result = a ^ b;
        Console.WriteLine(result);
    }
}
 
// This code is contributed by lokesh


Javascript




let a = 12;
let b = 25;
console.log((a ^ b));
// This code is contributed by akashish__


Output

21






Time Complexity: O(1) 
Auxiliary Space: O(1)

Complete Reference for Bitwise Operators in Programming/Coding

There exists no programming language that doesn’t use Bit Manipulations. Bit manipulation is all about these bitwise operations. They improve the efficiency of programs by being primitive, fast actions. There are different bitwise operations used in bit manipulation. These Bitwise Operators operate on the individual bits of the bit patterns. Bit operations are fast and can be used in optimizing time complexity.

Table of Content

  • 1. Bitwise AND Operator (&)
  • 2. ​Bitwise OR Operator (|)
  • 3. ​Bitwise XOR Operator (^)
  • 4. ​Bitwise NOT Operator (!~)
  • 5. Left-Shift (<<)
  • 6. Right-Shift (>>)

Some common bit operators are:

Bitwise Operator Truth Table

Similar Reads

1. Bitwise AND Operator (&)

The bitwise AND operator is denoted using a single ampersand symbol, i.e. &. The & operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0....

2. ​Bitwise OR Operator (|)

...

3. ​Bitwise XOR Operator (^)

...

4. ​Bitwise NOT Operator (!~)

...

5. Left-Shift (<<)

...

6. Right-Shift (>>)

...

Application of BIT Operators

The | Operator takes two equivalent length bit designs as boundaries; if the two bits in the looked-at position are 0, the next bit is zero. If not, it is 1....